package cn.kgc.jdbc.service;

import cn.kgc.jdbc.dao.OracleDaoImpl;
import cn.kgc.jdbc.dao.UserDao;
import cn.kgc.jdbc.dao.UserDaoImpl;
import cn.kgc.jdbc.entity.User;
import cn.kgc.jdbc.util.DruidUtil;
import cn.kgc.jdbc.util.Result;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.List;

/**
 * @author: mosin
 * @version: v1.0  2022/8/26
 * 用于业务处理类
 */
public class UserService {
    //注入UserDao
    UserDao userDao = new UserDaoImpl();
    //添加用户
    public Result addUser(User user){
        Result result = new Result();
        Integer line = userDao.addUser(user);
        if(line>0){
            result.setCode(200);
            result.setMsg("ok");
        }else{
            result.setCode(500);
            result.setMsg("error");
        }
        return result;
    }

    //查询用户
    public Result  findAllUsers(){
        Result result = new Result();
        try {
            List<User> all = userDao.findAll();
            result.setMsg("ok");
            result.setData(all);
            result.setCode(200);
        } catch (Exception e) {
            result.setMsg("error");
            result.setCode(500);
            e.printStackTrace();
        }
        return result;
    }

}
